<?php
//require_once 'goalbit_constants';

class Tasks_controller extends CI_Controller {

	function __construct(){
        parent::__construct();
        if($this->session->userdata('logged_in')==TRUE){
        	$this->load->model('Comment_model');
        	$this->load->model('task');	
        }else{
        	redirect('/index.php/authentication_controller');
        }
    }
    
	function index(){
		//$this->load->view('uploadform');
		$this->load->view('tasks_list_view');
	}
	/**
   	 * Saves a task into the database
   	 * @see CRUD_interface::save()
   	 */
	function save(){
		$id						= $this->input->post('id');
		$project_id				= $this->session->userdata('current_project');
		$sprint_id				= $this->input->post('sprint_id');
		$subject 				= $this->input->post('subject');
		$description 			= $this->input->post('description');
		$priority 				= $this->input->post('priority');
		$status 				= $this->input->post('status');
		$estimated_hours 		= $this->input->post('estimated_hours');
		$worked_hours 			= $this->input->post('worked_hours');
		$percentaje_completed 	= $this->input->post('percentaje_completed');
		$project				= $this->input->post('user_projects_select');
		$assigned_to_id			= $this->input->post('assigned_to');
		
			if($id != NULL && $id != ''){
				$this->task->update_task($id, $project_id, $sprint_id, $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
			}
			else{
				for($i=0; $i<sizeof($assigned_to_id); $i++){
					$this->task->create_task($project_id, $sprint_id, $assigned_to_id[$i], $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
				}	
			}
		redirect('/index.php/authentication_controller/main_view/1' );
	}
	
	function create() {
		$data['general'] = array('priority_options' => array('1' => '1', '2' => '2', '3' => '3'),
										 'status_options' 	=> array('0' => '0', '1' => '1', '2' => '2'),
										 'completed_options'=> array('0' => '0', '1' => '1', '2' => '2')
	);
										
		$this->load->view('task_upsert', $data);
	}
	
	function delete($id) {
		$this->task->delete_task($id);
		redirect('/index.php/tasks_controller', 'refresh');
	}
	
	function update($id = NULL) {
		$data['general'] = array('priority_options' => array('1' => '1', '2' => '2', '3' => '3'),
										 'status_options' 	=> array('0' => '0', '1' => '1', '2' => '2'),
										 'completed_options'=> array('0' => '0', '1' => '1', '2' => '2')
										);
		if($id != NULL && $id != ''){
			$task = $this->task->get_task($id);
			$data['task'] = $task[0];
		}
		
		$this->load->view('task_upsert', $data);
	}
	
	function get(){
		$id = $_POST['id'];
		$result = NULL;
		
		if($id != NULL && $id != ''){
			$task = $this->task->get_task($id);
			$result = $task[0];
		}
		echo json_encode($result);
	}
	
	
	public function get_json_tasks_list() {
   		$page 			= $this->input->post('page');
		$limit 			= $this->input->post('rows');
		$order_by_field = $this->input->post('sidx');
		$order_by_type 	= $this->input->post('sord');
		
		if(!$order_by_field) $sidx =1;
		
		$count = $this->task->get_tasks_count();

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit;
		
		$responce->page 	= $page;
		$responce->total 	= $total_pages;
		$responce->records 	= $count;
		
		$tasks = $this->task->get_tasks($order_by_field, $order_by_type, $start , $limit); 
		
		for($i=0; $i< sizeof($tasks); $i++) {
			$responce->rows[$i]['id'] = $tasks[$i]['id'];
			$responce->rows[$i]['cell'] = array($tasks[$i]['id'], 
												$tasks[$i]['subject'], 
												$tasks[$i]['priority'],
												$tasks[$i]['creation_date'],
												$tasks[$i]['status'],
												$tasks[$i]['estimated_hours'], 
												$tasks[$i]['worked_hours'], 
												$tasks[$i]['percentaje_completed'], 'action');
		}
     	
		echo json_encode($responce);
   	}
   	
	/**
	 * Displays task detail view
	 * @param $id - task id
	 */
   	public function detail_view($id = NULL){
   		$result = $this->task->get_task($id);
   		$data = $result[0];
   		//$data["page"] = 1000;//Goalbit_Constants::$TASK_DETAIL;
   		//$data["id"] = $id;
   		//$data["ref"]  = "../load_detail_view";  
   		$this->load->view('task_detail_view', $data);
   	}
   	
   	public function load_detail_view($id = NULL){
   		$result = $this->task->get_task($id);
   		$data = $result[0];
   		$this->load->view("task_detail_view",$data);
   	}
   	
   	
   	
	public function save_comment(){
		$this->load->model('comment_model');	
   		$this->load->helper('date');
   		
   		$comment 		= $this->input->post('comment');
		$ref_id			= $this->input->post('ref_id');
		$ref_type		= 'task';
		$created_by_id	= $this->session->userdata('user_id');
		$created_date	= date('Y-m-d h:m:s', now());
		
		
   		
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = '*';
		$config['max_size']	= '10000';
		$config['max_width']  = '10000';
		$config['max_height']  = '10000';
		$config['max_filename']  = '250';

		$this->load->library('upload', $config);
		$this->upload->do_upload();
		$upload_data = $this->upload->data();
		$this->Comment_model->create_comment($comment, $ref_id, $ref_type, $created_by_id, $created_date,$upload_data[file_name]);
		
		//$this->upload->do_upload();
		redirect('/index.php/tasks_controller/detail_view/'.$ref_id );
	}
   	
   	public function get_task_comments($task_id){
   		$this->load->model('comment_model');	
   		$comments = $this->comment_model->get_task_comments($task_id);
   		return $comments;
   	}
   	
   public function get_task_comments_json(){
   		$task_id = $_POST['task_id'];
   		$comments = $this->get_task_comments($task_id);
   		echo json_encode($comments);
   	}
   	
}
?>